<?php if (!defined('BASEPATH')) exit('No direct script access allowed');


class Customer extends Admin_Controller {
	
	function __construct()
	{
		parent::__construct();
		$this->admin_navigation->parent_active('customer');
	}
	function customer_manager(){
	 	$this->load->model('customers/customer_model');
		$columns = array(
						array(
							'name' => 'ID',
							'type' => 'id',
							'index'=>'customer_id',
							'sort_column'=>false
							),
						array(
							'name'=>$this->__('Phone Number'),
							'type'=>'text',
							'filter'=>'username',
							'index'=>'username',
							'sort_column'=>true
						),
						array(
							'name'=>$this->__('Created'),
							'type'=>'date',
							'index'=>'created',
						),
						array(
							'name'=>$this->__('Publish Down Vip'),
							'type'=>'date',
							'index'=>'publish_down_vip',
						),
						array(
							'name'=>$this->__('Status'),
							'type'=>'publish',
							'filter'=>'status',
							'index'=>'status',
							'options'=>array(
											 '0'=>'Tắt',
											 '1'=>'Bật'
											)
						),
		);
		$this->grid->columns($columns);
		$this->grid->datasource('customers/customer_model','get_customers');
		$this->grid->set_title($this->__('Customer Manager'));
		$this->grid->add_button($this->__('Add New'),admin_url('*/customer/add_customer'),'add');
		$this->grid->set_row_action('edit_customer');
		$this->grid->total_rows($this->customer_model->get_count());
		$this->grid->add_mass_action('delete', array(
			'label'=>$this->__('Delete'),
			'url'      => admin_url('*/customer/delete_customer'),
            'confirm'  =>$this->__('Are you sure ?')
		));
		//echo $this->db->last_query();
		if ($this->input->is_ajax()){
			$this->output->set_output($this->grid->display());
		}else{
			$this->load->view('admin/grid');
		}
	}
	
	function edit_customer(){
		
		$customer_id = $this->input->get_param('id',false);
		if ($customer_id){
			$this->load->model('customers/customer_model');
			$data = $this->customer_model->get_customer_by_id($customer_id);
			$this->form->set_header_text(__("Edit '%s'",$data['username']));
			$this->form->set_values($data);
		}
		$this->form->set_form_attributes(array(
			'id'=>'customer_form',
			'action'=>admin_url('*/customer/edit_post_customer',array('id'=>$game_id)),
			'enctype'=>'multipart/form-data'
		));
		$this->form->add_filedset('ganeral',array(
			'legend'=>'Ganeral'
		));
		$this->form->add_filed('username','text',array(
			'label'=>$this->__('Numberphone'),
			'name'=>'username',
			'required'=>true
		));
		$this->form->add_filed('password','password',array(
			'label'=>__('Password'),
			'id'=>'user_password',
			'name'=>'password',
			'required'=>true,
			'class'=>'required-entry validate-admin-password '
		));
		$this->form->add_filed('user_confirmation','password',array(
			'label'=>__('Password Confirmation'),
			'id'=>'user_confirmation',
			'name'=>'password_confirmation',
			'required'=>true,
			'id'=>'user_password',
			'class'=>'required-entry validate-cpassword '
		));
		$this->form->add_filed('status','select',array(
			'label'=>$this->__('Status'),
			'name'=>'status',
			'values'=>array('Tắt','Bật')
		));
		$this->load->view('admin/form',array('form'=>$this->form->to_html()));
	}
	
	function add_customer(){
	 	$this->_forward('edit_customer');
	}
	
	function edit_post_customer(){
		
	}
	
	function delete_customer(){
		$cids = explode(',',$this->input->get_param('customer_id',false));
		$this->load->model('customers/customer_model');
		foreach ($cids as $cid){
			$this->customer_model->delete_customer($cid);
		}
		$this->_redirect(admin_url('*/customer/customer_manager'));
	}
	
}